<template>
  <div class="rk-task-base-conf-body">
    <el-form class="base-conf-task"
      :model="queryParams"
      ref="baseRef"
      :rules="baseConfRules"
      label-width="auto">
      <el-form-item label="任务名称"
        prop="name">
        <el-input :disabled="isDisabled"
          v-model="queryParams.name"
          clearable
          placeholder="请输入任务名称" />
      </el-form-item>
      <el-form-item label="任务类型"
        prop="type">
        <rk-tree-select enum="EXPERT_INPUT_STATUS"
          :disabled="isDisabled"
          :filterable="false"
          :showCode="false"
          placeholder="请选择任务类型"
          v-model="queryParams.type">
        </rk-tree-select>
      </el-form-item>
      <template v-if="isDisabled">
        <el-form-item label="开始时间"
          prop="startDate">
          <el-date-picker :disabled="isDisabled"
            v-model="queryParams.startDate"
            placeholder="请选择开始时间"
            clearable
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            type="date" />
        </el-form-item>
      </template>
      <template v-if="isDisabled">
        <el-form-item label="结束时间"
          prop="endDate">
          <el-date-picker :disabled="isDisabled"
            v-model="queryParams.endDate"
            placeholder="请选择结束时间"
            clearable
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            type="date" />
        </el-form-item>
      </template>
      <el-form-item label="备注">
        <el-input :disabled="isDisabled"
          v-model="queryParams.remark"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 8 }"
          placeholder="请输入备注" />
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup>
import { nextTick, reactive, ref, watch } from "vue";
import XEUtils from 'xe-utils'

const props = defineProps({
  isDisabled: {
    type: Boolean,
    default: false
  },
  taskDetailData: { type: [Object], default: () => ({}) }
})
const baseRef = ref(null)
let queryParams = reactive({
  name: undefined,
  startDate: undefined,
  endDate: undefined,
  type: undefined,
  remark: undefined
})
watch(() => props.taskDetailData, async (newValue) => {
  if (!XEUtils.isEmpty(newValue)) {
    await nextTick()
    queryParams.name = newValue.value.name
    queryParams.startDate = newValue.value.startDate
    queryParams.endDate = newValue.value.endDate
    queryParams.remark = newValue.value.remark
    queryParams.type = newValue.value.type
  } else {
    queryParams = reactive({
      name: undefined,
      startDate: undefined,
      endDate: undefined,
      type: undefined,
      remark: undefined
    })
  }
}, { deep: true, immediate: false })
if (!props.isDisabled) {
  const sendType = inject('sendHandleType');
  watch(() => queryParams.type, (newValue) => {
    if (newValue) {
      sendType(queryParams.type)
    }
  }, { deep: true, immediate: false })
}
const baseConfRules = reactive({
  name: [
    { required: true, message: '请输入任务名称', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择任务类型', trigger: 'change' }
  ],
  startDate: [
    { required: true, message: '请选择开始时间', trigger: 'change' }
  ],
  endDate: [
    { required: true, message: '请选择结束时间', trigger: 'change' }
  ],
})
// 验证表单是否通过
const handleSubmitFormValidate = async () => {
  let isValidate = false
  await baseRef.value.validate((valid, fields) => {
    if (valid) {
      isValidate = true
    } else {
      isValidate = false
    }
  })
  return isValidate
}
// 重置表单
const resetForm = () => {
  queryParams = reactive({})
  baseRef.value.resetFields()
}
// 获取表单数据
const handleBaseFromData = async () => {
  const isValidate = await handleSubmitFormValidate()
  const baseData = {
    isBaseValidate: isValidate,
    baseForm: queryParams,
  }
  return baseData
}
// 对外暴露方法
defineExpose({
  handleBaseFromData,
  resetForm
})
</script>
<style lang="scss" scoped>
.rk-task-base-conf-body {
  height: 100%;
  background-color: #fff;
  padding-top: 50px;
  display: flex;
  justify-content: center;
  .base-conf-task {
    width: 60%;
  }
}
</style>